#include <stdio.h>
#include <stdbool.h>
#include "test.h"
#include "testTypeSupport.h"
#include "dds_domain_participant_factory.h"
#include "dds_domain_participant.h"
#include "dds_subscriber.h"
#include "dds_topic.h"
#include "dds_datareader.h"
#include "dds_infrastructure.h"
#include <string.h>

int main(int argc, char* argv[]) 
{
    // C接口创建DomainParticipantFactory
    DDS_DomainParticipantFactory* factory = DDS_DomainParticipantFactory_get_instance();
    if (factory == NULL) {
        printf("Failed to get DomainParticipantFactory instance\n");
        return -1;
    }
    // 创建DomainParticipant
    DDS_DomainParticipant* participant = DDS_DomainParticipantFactory_create_participant(factory, 0, DDS_DOMAINPARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("Failed to create DomainParticipant\n");
        return -1;
    }
    // 创建订阅者
    DDS_Subscriber* subscriber = DDS_DomainParticipant_create_subscriber(participant, DDS_SUBSCRIBER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (subscriber == NULL) {
        printf("Failed to create Subscriber\n");
        return -1;
    }
    // 注册类型
    void* testTypeSupport = test_ComplexStructTypeSupport_get_instance();
    if (testTypeSupport == NULL) {
        printf("Failed to get TypeSupport instance\n");
        return -1;
    }
    DDS_ReturnCode_t retcode = DDS_DomainParticipant_registe_type(participant, "test_ComplexStructTypeSupport", testTypeSupport);
    if (retcode != DDS_RETCODE_OK) {
        printf("Failed to register type\n");
        return -1;
    }
    // 创建主题
    DDS_Topic* topic = DDS_DomainParticipant_create_topic(participant, "test_ComplexStruct", "test_ComplexStructTypeSupport", NULL, NULL, DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("Failed to create Topic\n");
        return -1;
    }
    DDS_TopicDescription* topicDesc = DDS_Topic_as_topicdescription(topic);
    // 创建数据读取器
    DDS_DataReader* reader = DDS_Subscriber_create_datareader(subscriber, topicDesc, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (reader == NULL) {
        printf("Failed to create DataReader\n");
        return -1;
    }
    // 数据接收循环
    test_ComplexStructSequence* data_seq = test_ComplexStructSequence_create();
    DDS_SampleInfoSequence* info_seq = DDS_SampleInfoSequence_create();
    while (true) {
        retcode = DDS_DataReader_take(reader, &data_seq, &info_seq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
        if (retcode == DDS_RETCODE_NO_DATA) {
            continue;
        } else if (retcode != DDS_RETCODE_OK) {
            printf("Failed to take data\n");
            return -1;
        }
        for (int i = 0; i < DDS_SampleInfoSequence_size(info_seq); i++) {
            DDS_SampleInfo* info = DDS_SampleInfoSequence_at(info_seq, i);
            if (!info->valid_data) {
                continue;
            }
            test_ComplexStruct *data = test_ComplexStructSequence_at(data_seq, i);
        }
        DDS_DataReader_return_loan(reader, &data_seq, &info_seq);
    }
    return 0;
}
